What is Variadic Templates
-
A template with at least one
parameter pack
is called a variadic template.
Types of Variadic Templates
Variadic Class Template
1 Argument
2 or more arguments
-
Template parameter pack must be the final parameter in the template parameter list.
template <typename U, typename... Ts> // OK: can deduce U
class A {};
// template <typename... Ts, typename U> // Error: Ts... not at the end
// class A {};
Variadic Function Template
-
Variadic function template means function taking a parameter pack
... is Left Fold Expression?
template <class ... T>
void f1(T ... val) {
(cout << ... << val); //Expanded to cout << arg1 << arg2 ..
}
int main(){
f1('a'); //Expanded as f1 <char> () //output = a
f1(2," cd "); //f1 <char, int, char const*> ()//output = 2 cd
}